﻿using UnityEngine;
using System.Collections;

public partial class MessageScene : Scene
{
	[HideInInspector] public long receiverId;
	public UILabel receiverNameLabel;

	string initialReceiverNameLabelText;
	string initialMessageLabelText;

	public UIInput messageInput;


	public void OnClick_FindFriend()
	{
		EPLog.Log("OnClick_FindFriend");
		Popup_SelectFriend pf = PopupManager.PushPopup(PopupKind.SelectFriend) as Popup_SelectFriend;
		pf.sceneObject = this.gameObject;
	}

	public void OnClick_WriteMessageConfirm()
	{
		EPLog.Log("OnClick_WriteMessageConfirm");

		if (receiverId == 0 && string.Compare(receiverNameLabel.text.Trim(), initialReceiverNameLabelText) == 0)
		{
			PopupUtility.OneButtonErrorPopup("No Name");
			return;
		}

		if (string.Compare(messageInput.label.text.Trim(), initialMessageLabelText) == 0 || messageInput.label.text.Trim().Length <= 0)
		{
			PopupUtility.OneButtonErrorPopup("No Message");
			return;
		}

		Popup_TwoButton pt = PopupManager.PushPopup(PopupKind.TwoButton) as Popup_TwoButton;
		pt.SetPopupWithTitleAndBody("Confirm", "메시지를 보내시겠습니까?");
		pt.SetEventOK(gameObject, "OnClick_WriteMessageConfirmPostProcess");
	}

	public void OnClick_WriteMessageConfirmPostProcess()
	{
		EPLog.Log("OnClick_WriteMessageConfirmPostProcess");
		NetworkManager.Instance.C2S_MSG_SMAIL(receiverId, UnityEngine.WWW.EscapeURL(messageInput.label.text), (responseData) => {
			
			if (responseData.ecode != 0)
			{
				PopupUtility.OneButtonErrorPopup(responseData.ecode);
				return;
			}

			Popup_OneButton po = PopupManager.PushPopup(PopupKind.OneButton) as Popup_OneButton;
			po.SetPopupWithTitleAndBody("", "성공적으로 메시지를 보냈습니다.");
			po.SetEventOK(gameObject, "확인");
		});
	}

	public void OnClick_WriteMessageCancel()
	{
		EPLog.Log("OnClick_WriteMessageCancel");

		messageInput.value = string.Empty;
		messageInput.label.text = initialMessageLabelText;
		receiverNameLabel.text = initialReceiverNameLabelText;

		RestoreCurrentTab();
	}

	public void OnClick_Confirm()
	{
		PopupManager.PopPopup();

		messageInput.value = string.Empty;
		messageInput.label.text = initialMessageLabelText;
		receiverNameLabel.text = initialReceiverNameLabelText;

		RestoreCurrentTab();
	}
}
